 function  [daux,min,max,gain,Haux] = maximizedin(Haux,distribution,w)
           
    gaindelta=1.1;

    etapas = length(distribution);
    gain = ones(etapas);
    gain = gain(1,:);
    daux=inf;
    Vec = [];
    
    for k = 1: etapas
        [mag] = bode(Haux(k), w);
        mag = squeeze(mag);
        Vec = [Vec mag];
    end
    
%     for m = 1:2
        for k=1:etapas
            for l=1:etapas 
                if k~=l           
%                         Vec(:,distribution(k)) = Vec(:,distribution(k))*gaindelta;
%                         Vec(:,distribution(l)) = Vec(:,distribution(l))/gaindelta;
%                         gain(k)=gain(k)*gaindelta;
%                         gain(l)=gain(l)/gaindelta;
                        d = dinamRange(Vec,distribution);
%                         if distribution(1) == 1 && distribution(2) == 2 && distribution(3) == 3 && distribution(4) == 4
%                             d
%                         end
                        

                        while d <= daux
                            daux = dinamRange(Vec,distribution);
                            Vec(:,distribution(k)) = Vec(:,distribution(k))*gaindelta;
                            Vec(:,distribution(l)) = Vec(:,distribution(l))/gaindelta;
                            gain(k)=gain(k)*gaindelta;
                            gain(l)=gain(l)/gaindelta;
                            d = dinamRange(Vec,distribution);
                        end

                        Vec(:,distribution(k)) = Vec(:,distribution(k))/gaindelta;
                        Vec(:,distribution(l)) = Vec(:,distribution(l))*gaindelta;
                        gain(k) = gain(k)/gaindelta;
                        gain(l) = gain(l)*gaindelta;

                        d = dinamRange(Vec,distribution);
                        entered = 0;
                        
                        while d <= daux
                            entered = 1;
                            daux = dinamRange(Vec,distribution);
                            Vec(:,distribution(k)) = Vec(:,distribution(k))/gaindelta;
                            Vec(:,distribution(l)) = Vec(:,distribution(l))*gaindelta;
                            gain(k)=gain(k)/gaindelta;
                            gain(l)=gain(l)*gaindelta;
                            d = dinamRange(Vec,distribution);
                        end

                        if entered
                            Vec(:,distribution(k)) = Vec(:,distribution(k))*gaindelta;
                            Vec(:,distribution(l)) = Vec(:,distribution(l))/gaindelta;
                            gain(k)=gain(k)*gaindelta;
                            gain(l)=gain(l)/gaindelta;
                        end
                end

            end

        end
%     end
    
    for k=1:etapas
        %disp('hi')
        Haux(distribution(k)) = Haux(distribution(k))*gain(k);
    end
    
%     if distribution(1) == 1 && distribution(2) == 2 && distribution(3) == 3 && distribution(4) == 4
%         daux
%         d
%     end
    [daux max min] = dinrange(Haux,w,distribution);
%     if distribution(1) == 1 && distribution(2) == 2 && distribution(3) == 3 && distribution(4) == 4
%         daux
%         d
%     end
 end

 function d = dinamRange(Vec,config)
     minout = inf;
     maxout = 0;
     dimen = size(Vec);
     TransferAux = ones(dimen(1),1);
     for k = 1: dimen(2)
         TransferAux = TransferAux.*Vec(:,config(k));
         maxaux = max(TransferAux);
         minaux = min(TransferAux);
         
         if maxaux > maxout
             maxout = maxaux;
         end
         
         if minaux < minout
             minout = minaux;
         end
     end
     
     d = maxout/minout;
 end
 